package com.ruoyi.housingresources.service.impl;

import java.util.List;

import com.ruoyi.building.domain.AmBuilding;
import com.ruoyi.building.mapper.AmBuildingMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.domain.AmProject;
import com.ruoyi.project.mapper.AmProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.housingresources.mapper.AmHousingResourcesMapper;
import com.ruoyi.housingresources.domain.AmHousingResources;
import com.ruoyi.housingresources.service.IAmHousingResourcesService;

/**
 * 房源管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-28
 */
@Service
public class AmHousingResourcesServiceImpl implements IAmHousingResourcesService 
{
    @Autowired
    private AmHousingResourcesMapper amHousingResourcesMapper;

    @Autowired
    private AmBuildingMapper amBuildingMapper;

    @Autowired
    private AmProjectMapper projectMapper ;

    /**
     * 查询房源管理
     * 
     * @param id 房源管理ID
     * @return 房源管理
     */
    @Override
    public AmHousingResources selectAmHousingResourcesById(Long id)
    {
        return amHousingResourcesMapper.selectAmHousingResourcesById(id);
    }

    /**
     * 查询房源管理列表
     * 
     * @param amHousingResources 房源管理
     * @return 房源管理
     */
    @Override
    public List<AmHousingResources> selectAmHousingResourcesList(AmHousingResources amHousingResources)
    {
        amHousingResources.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
        return amHousingResourcesMapper.selectAmHousingResourcesList(amHousingResources);
    }

    /**
     * 新增房源管理
     * 
     * @param amHousingResources 房源管理
     * @return 结果
     */
    @Override
    public int insertAmHousingResources(AmHousingResources amHousingResources)
    {
        List<String> list = amHousingResourcesMapper.selectAllAmHousingResourceList();
        AmBuilding building = amBuildingMapper.selectAmBuildingById(amHousingResources.getBuildingId());
        AmProject project = projectMapper.selectAmProjectById(building.getProjectId());
        if (list.contains(project.getProjectName() + building.getBuildingName() + amHousingResources.getRoomNum())) {
            throw  new CustomException("该房号已存在,请勿重复添加");
        }
        amHousingResources.setCreateTime(DateUtils.getNowDate());
        return amHousingResourcesMapper.insertAmHousingResources(amHousingResources);
    }

    /**
     * 导入房源
     *
     * @param list
     * @param updateSupport
     */
    @Override
    public String importProject(List<AmHousingResources> list, boolean updateSupport, SysUser user) {
        // 查询所有楼宇
        List<AmBuilding> buildings = amBuildingMapper.selectAmBuildingList(null);

        for(AmHousingResources amHousingResources :list) {
            try {
                amHousingResources.setBuildingId(AmBuilding.castIdByBuildingName(buildings,amHousingResources.getBuildingName(),amHousingResources.getProjectName()));
                amHousingResourcesMapper.insertAmHousingResources(amHousingResources);
            } catch (Exception e) {
                e.printStackTrace();
                return "插入失败,请检查数据有效性";
            }
        }
        return "插入成功";
    }

    /**
     * 修改房源管理
     * 
     * @param amHousingResources 房源管理
     * @return 结果
     */
    @Override
    public int updateAmHousingResources(AmHousingResources amHousingResources)
    {
        List<String> list = amHousingResourcesMapper.selectAllAmHousingResourceList();
        AmBuilding building = amBuildingMapper.selectAmBuildingById(amHousingResources.getBuildingId());
        AmProject project = projectMapper.selectAmProjectById(building.getProjectId());
        if (list.contains(project.getProjectName() + building.getBuildingName() + amHousingResources.getRoomNum())) {
            throw  new CustomException("该房号已存在,请勿重复添加");
        }
        return amHousingResourcesMapper.updateAmHousingResources(amHousingResources);
    }

    /**
     * 批量删除房源管理
     * 
     * @param ids 需要删除的房源管理ID
     * @return 结果
     */
    @Override
    public int deleteAmHousingResourcesByIds(Long[] ids)
    {

        amHousingResourcesMapper.deleteCollections(ids);
        return amHousingResourcesMapper.deleteAmHousingResourcesByIds(ids);
    }

    /**
     * 删除房源管理信息
     * 
     * @param id 房源管理ID
     * @return 结果
     */
    @Override
    public int deleteAmHousingResourcesById(Long id)
    {
        return amHousingResourcesMapper.deleteAmHousingResourcesById(id);
    }

    /**
     * 查询所有房源全称
     */
    @Override
    public List<String> selectAllAmHousingResourceList() {
        return amHousingResourcesMapper.selectAllAmHousingResourceList();
    }
}
